package jsyf.oa.dao.psi;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import jsyf.oa.entity.psi.PurchaseManiEntity;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

import jsyf.oa.idao.BaseDao;
import jsyf.oa.ientity.IBaseEntity;

/**
 * Created by Maqiuqiu on 2018/10/20.
 */
@Repository
public class PurchaseDao extends BaseDao {

    public PurchaseDao() {
        super.iBaseEntity = new PurchaseManiEntity();
    }

    public List<PurchaseManiEntity> getEntityListByCondition(String condition, Object[] params) {
        List<JSONObject> jsonList = getJSONListByCondition(condition, params);
        List<PurchaseManiEntity> entityList = new ArrayList<>();
        for (int i = 0; i < jsonList.size(); i++) {
            PurchaseManiEntity entity = (PurchaseManiEntity) convertJson2Entity(jsonList.get(i));
            entityList.add(entity);
        }
        return entityList;
    }

    @Override
    public IBaseEntity convertJson2Entity(JSONObject json) {
        return JSON.toJavaObject(json, PurchaseManiEntity.class);
    }

    public List<JSONObject> getPurchaseList(String userId,String condition, Integer requestPage, Integer pageSize){
        StringBuffer sql = new StringBuffer();
        sql.append("  SELECT\n" +
                "\tt_psi_purchase.ID AS id,\n" +
                "\tt_psi_purchase.APPLY_DATE AS applyDate,\n" +
                "\tt_psi_purchase.COMPANY_NAME AS companyName,\n" +
                "\tt_psi_purchase.DEPART_NAME AS departName,\n" +
                "  t_psi_purchase.RECEIVE_DATE AS receiveDate,\n" +
                "  t_psi_purchase.PURPOSE AS purpose,\n" +
                "  t_psi_purchase.METHOD AS method,\n" +
                "  t_psi_purchase.STATE AS state,\n" +
                "\tt_psi_purchase.AUDIT_FLAG AS auditFlag,\n" +
                "\tt_psi_purchase.CURRENT_AUDITOR_ID AS currentAuditorId,\n" +
                "\tt_psi_purchase.CURRENT_AUDITOR_NAME AS currentAuditorName,\n" +
                "\tt_psi_purchase.CREATE_USER_ID AS createUserId,\n" +
                "  t_psi_purchase.CREATE_USER_NAME AS createUserName,\n" +
                "  t_psi_purchase.BUYER_USER_ID AS buyerUserId,\n" +
                "  t_psi_purchase.BUYER_USER_NAME AS buyerUserName,\n" +
                "  t_psi_purchase.BUYER_DATE AS buyerDate,\n" +
                "  t_psi_purchase.BUYER_ATTACH_NAME AS buyerAttachName,\n" +
                "  t_psi_purchase.BUYER_ATTACH_URL AS buyerAttachUrl\n" +
                "FROM\n" +
                "\tt_psi_purchase\n" +
                "WHERE\n" +
                "\t1 = 1\n" +
                "AND (\n" +
                "\tt_psi_purchase.CREATE_USER_ID = '"+userId+"'\n" +
                "\tOR (\n" +
                "\t\tt_psi_purchase.ID IN (\n" +
                "\t\t\tSELECT\n" +
                "\t\t\t\tMAIN_ID\n" +
                "\t\t\tFROM\n" +
                "\t\t\t\tt_sys_audit\n" +
                "\t\t\tWHERE\n" +
                "\t\t\t\tAUDITOR_ID = '"+userId+"'\n" +
                "\t\t)\n" +
                "\t\tAND t_psi_purchase.AUDIT_FLAG IN ('1', '0')\n" +
                "\t)\n" +
                "\tOR (\n" +
                "\t\tt_psi_purchase.ID IN (\n" +
                "\t\t\tSELECT\n" +
                "\t\t\t\tMAIN_ID\n" +
                "\t\t\tFROM\n" +
                "\t\t\t\tt_sys_copier\n" +
                "\t\t\tWHERE\n" +
                "\t\t\t\tCOPIER_ID = '"+userId+"'\n" +
                "\t\t)\n" +
                "\t\tAND t_psi_purchase.AUDIT_FLAG = '1'\n" +
                "\t)\n" +
                ")   ");
        if (condition != null){
            sql.append(condition);
        }
        super.appendPageSql(sql, requestPage, pageSize);
        return super.queryForJsonList(sql.toString());
    }

    public List<JSONObject> getGoodsInfo(String purchaseId){
        StringBuffer sql = new StringBuffer();
        sql.append(" SELECT\n" +
                "  t_psi_inventory.ID AS id,\n" +
                "  t_psi_purchase.ID AS purchaseId,\n" +
                "  t_psi_inventory.GOODS_ID AS goodsId,\n" +
                "  t_psi_goods.`NAME` AS goodsName,\n" +
                "  t_psi_inventory.UNIT AS unit,\n" +
                "  t_psi_inventory.COUNT AS count,\n" +
                "  t_psi_inventory.PRICE AS price,\n" +
                "  t_psi_inventory.EXTRA AS extra,\n" +
                "  t_psi_inventory.REQUIREMENTS AS requirements \n" +
                "FROM t_psi_inventory\n" +
                "LEFT JOIN t_psi_purchase ON t_psi_inventory.PURCHASE_ID = t_psi_purchase.ID\n" +
                "LEFT JOIN t_psi_goods ON t_psi_inventory.GOODS_ID = t_psi_goods.ID\n" +
                "WHERE 1 = 1\n" +
                "AND t_psi_purchase.ID = '"+purchaseId+"' ");
        return super.queryForJsonList(sql.toString());
    }
}

